<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实验编号" prop="experimentNumber">
        <el-input
          v-model="queryParams.experimentNumber"
          placeholder="请输入实验编号"
          clearable
          @keyup.enter.native="handleQuery"
          type="number"
        />
      </el-form-item>
      <el-form-item label="色系" prop="colorSchemeId">
        <el-select v-model="queryParams.colorSchemeId"
                   placeholder="请输入色系"
                   @keyup.enter.native="handleQuery"
                   clearable
                   filterable>
          <el-option
            v-for="material in colorSchemeList"
            :key="material.colorScheme_id"
            :label="material.name"
            :value="material.colorScheme_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="色号" prop="clorlNumber">
        <el-input
          v-model="queryParams.clorlNumber"
          placeholder="请输入色号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺" prop="artId">
        <el-select v-model="queryParams.artId" placeholder="请输入采用的工艺"
                   clearable
                   @keyup.enter.native="handleQuery"
                   filterable
        >
          <el-option
            v-for="material in artTableList"
            :key="material.id"
            :label="material.name"
            :value="material.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原料" prop="materialId">
        <el-select v-model="queryParams.materialId"
                   placeholder="请输入配方成分"
                   clearable
                   @keyup.enter.native="handleQuery"
                   filterable>
          <el-option
            v-for="material in materialList"
            :key="material.material_id"
            :label="material.name"
            :value="material.material_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原料重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入原料重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unitId">
        <el-input
          v-model="queryParams.unitId"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配方" prop="formulaId">
        <el-select v-model="queryParams.formulaId"
                   placeholder="请输入配方成分"
                   clearable
                   @keyup.enter.native="handleQuery"
                   filterable>
          <el-option
            v-for="material in recieptNameList"
            :key="material.recieptName_id"
            :label="material.name"
            :value="material.recieptName_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="CO2流量kg/h" prop="co2">
        <el-input
          v-model="queryParams.co2"
          placeholder="请输入CO2流量kg/h"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度℃" prop="temperature">
        <el-input
          v-model="queryParams.temperature"
          placeholder="请输入温度℃"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压强Mpa" prop="pressure">
        <el-input
          v-model="queryParams.pressure"
          placeholder="请输入压强Mpa"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="timeConsuming">
        <el-input
          v-model="queryParams.timeConsuming"
          placeholder="请输入时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyiyinzheng:experimentRequest:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyiyinzheng:experimentRequest:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyiyinzheng:experimentRequest:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ruoyiyinzheng:experimentRequest:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="experimentRequestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="实验编号" align="center" prop="experimentNumber" />
      <el-table-column label="色系" align="center" prop="colorSchemeName" />
      <el-table-column label="色号" align="center" prop="clorlNumber" />
      <el-table-column label="工艺" align="center" prop="art" />
      <el-table-column label="原料" align="center" prop="materialName" />
      <el-table-column label="原料重量" align="center" prop="weight" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="染料配方" align="center" prop="receiptName" />
      <el-table-column label="成份集" align="center" prop="recipes" v-if="false"/>
      <el-table-column label="比例集" align="center" prop="rations" v-if="false"/>
      <el-table-column label="CO2流量kg/h" align="center" prop="co2" />
      <el-table-column label="温度℃" align="center" prop="temperature" />
      <el-table-column label="压强Mpa" align="center" prop="pressure" />
      <el-table-column label="时间" align="center" prop="timeConsuming" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyiyinzheng:experimentRequest:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyiyinzheng:experimentRequest:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleViewExperimentResults(scope.row)"
            v-hasPermi="['ruoyiyinzheng:experimentRequest:edit']"
          >实验结果</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实验要求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body >
      <el-tabs v-model="activeName" @tab-click="handleClick" >
        <el-tab-pane label="实验要求" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <!-- 左侧列 -->
            <el-col :span="12">
              <el-form-item label="实验编号" prop="experimentNumber">
                <el-input v-model="form.experimentNumber" placeholder="请输入实验编号" type="number"/>
              </el-form-item>
              <el-form-item label="色系" prop="colorSchemeId">
                <el-select v-model="form.colorSchemeId" placeholder="请输入色系">
                  <el-option
                    v-for="material in colorSchemeList"
                    :key="material.colorScheme_id"
                    :label="material.name"
                    :value="material.colorScheme_id"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="色号" prop="clorlNumber">
                <el-input v-model="form.clorlNumber" placeholder="请输入色号" />
              </el-form-item>
              <el-form-item label="工艺" prop="artId">
                <el-select v-model="form.artId" placeholder="请输入采用的工艺">
                  <el-option
                    v-for="material in artTableList"
                    :key="material.id"
                    :label="material.name"
                    :value="material.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="原料" prop="materialId">
                <el-select v-model="form.materialId" placeholder="请输入配方成分">
                  <el-option
                    v-for="material in materialList"
                    :key="material.material_id"
                    :label="material.name"
                    :value="material.material_id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="原料重量" prop="weight">
                <el-input v-model="form.weight" placeholder="请输入原料重量" />
              </el-form-item>
              <el-form-item label="计量单位" prop="unitId">
              <el-select v-model="form.unitId" placeholder="请输入配方成分">
                <el-option
                  v-for="material in unitList"
                  :key="material.id"
                  :label="material.name"
                  :value="material.id"
                ></el-option>
              </el-select>
              </el-form-item>
              <!-- 其他左侧表单项 -->
            </el-col>
            <!-- 右侧列 -->
            <el-col :span="12">
              <el-form-item label="配方" prop="formulaId">
                <el-select v-model="form.formulaId" placeholder="请输入配方成分" @change="updateTableData">
                  <el-option
                    v-for="material in recieptNameList"
                    :key="material.recieptName_id"
                    :label="material.name"
                    :value="material.recieptName_id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="CO2流量kg/h" prop="co2">
                <el-input v-model="form.co2" placeholder="请输入CO2流量kg/h" />
              </el-form-item>
              <el-form-item label="温度℃" prop="temperature">
                <el-input v-model="form.temperature" placeholder="请输入温度℃" />
              </el-form-item>
              <el-form-item label="压强Mpa" prop="pressure">
                <el-input v-model="form.pressure" placeholder="请输入压强Mpa" />
              </el-form-item>
              <el-form-item label="时间" prop="timeConsuming">
                <el-input v-model="form.timeConsuming" placeholder="请输入时间" />
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        </el-tab-pane>
        <el-tab-pane label="配方管理" name="second">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="配方" prop="formulaId">
          <el-select v-model="form.formulaId" placeholder="请输入配方成分" @change="updateTableData">
            <el-option
              v-for="material in recieptNameList"
              :key="material.recieptName_id"
              :label="material.name"
              :value="material.recieptName_id"
            ></el-option>
          </el-select>
<!--            <el-form-item label="当前配方：" />-->
        </el-form-item>
          </el-form>
     <st_table  :receiept-list="tableData"></st_table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listExperimentRequest, getExperimentRequest, delExperimentRequest, addExperimentRequest, updateExperimentRequest,listRecipesRequest } from "@/api/ruoyiyinzheng/experimentRequest";
import {listMaterial} from "@/api/ruoyiyinzheng/material";
import { listColorScheme } from "@/api/ruoyiyinzheng/colorScheme";
import { listRecieptName } from "@/api/ruoyiyinzheng/recieptName";
import st_table from "@/views/ruoyiyinzheng/experimentRequest/table.vue";
import { listArtTable } from "@/api/ruoyiyinzheng/artTable";
import { listUnit} from "@/api/ruoyiyinzheng/unit";
export default {
  name: "ExperimentRequest",
  components:{st_table},
  data() {
    return {

      // 表格数据
      tableData: [],
      unitList:[],
      artTableList:[],
      recieptNameList:[],// 下拉框的选项列表
      materialList:[],
      colorSchemeList:[],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验要求表格数据
      experimentRequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      activeName:'first',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        experimentNumber: undefined,
        colorSchemeId: undefined,
        clorlNumber: undefined,
        artId: undefined,
        materialId: undefined,
        weight: undefined,
        unitId: undefined,
        formulaId: undefined,
        co2: undefined,
        temperature: undefined,
        pressure: undefined,
        timeConsuming: undefined,
        rations:undefined,
      },
      // 表单参数
      form: {
        formulaId: null, // 下拉框的数据绑定
      },

      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        experimentNumber: [
          { required: true, message: "实验编号不能为空", trigger: "blur" }
        ],
        colorSchemeId: [
          { required: true, message: "色系ID不能为空", trigger: "blur" }
        ],
        clorlNumber: [
          { required: true, message: "色号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getMaterialList();
    this.getlistColorSchemeList();
    this.getlistRecieptNameList();
    this.getArtList();
    this.getUnitList();
  },
  methods: {
    /** 查询实验结果操作 */
    handleViewExperimentResults: function (row) {
      const experimentNumber = row.experimentNumber;
      console.log(experimentNumber+'点击查询');
      this.$router.push({path: "/process/manage/experimentResult/", query: {experimentNumber: experimentNumber}});
    },

    updateTableData(value) {
      listRecipesRequest(value).then(
        (rs)=>{
          this.tableData=[]
          let d=  rs.data;
          d.forEach(
            (e)=>{
              this.tableData.push({
              id: this.form.experimentNumber,
              recipe: e,
              ration: '0'
            })
          })
    })},



    handleClick(tab, event) {
      console.log(tab, event);
    },
    getUnitList()
    {
      listUnit().then(response => {
        this.unitList =  response.rows.map(item => ({
          id: item.id,
          name: item.name,
        }));
      });
    },
    getArtList()
    {
      listArtTable().then(response => {
        this.artTableList =  response.rows.map(item => ({
          id: item.id,
          name: item.name,
        }));
      });
    },
    getlistRecieptNameList()
    {
      listRecieptName().then(response => {
          this.recieptNameList =  response.rows.map(item => ({
            recieptName_id: item.id,
            name: item.stReceiptName,
          }));
    })},
    getlistColorSchemeList()
    {listColorScheme().then(response => {
      this.colorSchemeList =  response.rows.map(item => ({
        colorScheme_id: item.id,
        name: item.colorSchemeName,
      }));
    });
    },
    getMaterialList()
    {
      listMaterial().then(response => {
        this.materialList =  response.rows.map(item => ({
          material_id: item.id,
          name: item.name,
        }));
      });
    },
    /** 查询实验要求列表 */
    getList() {
      this.loading = true;
      listExperimentRequest(this.queryParams).then(response => {
        this.experimentRequestList = response.rows;
        console.log(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        experimentNumber: undefined,
        colorSchemeId: undefined,
        clorlNumber: undefined,
        artId: undefined,
        materialId: undefined,
        weight: undefined,
        unitId: undefined,
        formulaId: undefined,
        co2: undefined,
        temperature: undefined,
        pressure: undefined,
        timeConsuming: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        recipes: undefined,
        rations:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验要求";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.tableData=[]
      const id = row.id || this.ids
      let d=undefined;
      if (row.formulaId!=undefined&&this.form.recipes==undefined)
      {
        listRecipesRequest(row.formulaId).then(
          (rs)=>{
            d=  rs.data;
          })
      }

      getExperimentRequest(id).then(response => {
        this.loading = false;
        this.form = response.data;
       // let recipes=d==undefined?[]:d;
        let recipes=this.form.recipes==undefined?[]:this.form.recipes.split(',');
        if (d!=undefined){
          recipes=d;
        }
       let rations=this.form.rations==undefined?[]:this.form.rations.split(',');
        console.log(recipes.length+'dsad');
        for (let i = 0; i <recipes.length; i++) {
          this.tableData.push({
            id: this.form.experimentNumber,
            recipe: recipes[i],
            ration: rations[i]==[]||rations[i]==undefined?0:rations[i]});

        }
        this.open = true;
        this.title = "修改实验要求";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            this.form.recipes=this.tableData.map((r)=>{return r.recipe;}).join(',')
            this.form.rations=this.tableData.map((r)=>{return r.ration;}).join(',')
            updateExperimentRequest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addExperimentRequest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实验要求编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delExperimentRequest(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ruoyiyinzheng/experimentRequest/export', {
        ...this.queryParams
      }, `experimentRequest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
